package com.zzyq.pc.mongodb.Service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.common.plugin.shiro.TokenManager;
import com.zzyq.pc.mongodb.Service.IMonitoringService;
import com.zzyq.pc.mongodb.base.Basics;
import com.zzyq.pc.mongodb.dao.MongodbMonitoring;
import com.zzyq.pc.mongodb.model.MongodbDate;
import com.zzyq.pc.web.configure.base.Menu;
import com.zzyq.pc.web.configure.mapper.EnshrineMapper;
import com.zzyq.pc.web.configure.mapper.InformationMapper;
import com.zzyq.pc.web.configure.model.Information;
@Service
public class MonitoringService implements IMonitoringService{
	MongodbMonitoring mongodb = new MongodbMonitoring();
	
	@Autowired
	EnshrineMapper enshrineMapper;
	@Autowired
	InformationMapper informationMapper;
	//转载数和星级类
	Basics bs = new Basics();
	
	/**
	 * 监控——舆情概况
	 * @param pageView 分页参数
	 * @param ifmIds 方案ID 
	 * @param startTime 开始时间
	 * @param endTime	结束时间
	 */
	@Override
	public List<MongodbDate> findGeneralSituation(PageView pageView,Date startTime,
			Date endTime,List<String> ifmIds){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		List<MongodbDate> list1 = new ArrayList<MongodbDate>();
		//获取当前用户id
		int userId = TokenManager.getUser().getUserId();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//返回参数集合
		List<MongodbDate> mdlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds1 = new ArrayList<String>();
		//敏感方案ID集合
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information com : info) {
			//敏感方案
			if (com.getIfmIssensitivity()==1) {
				ifmIds2.add(String.valueOf(com.getIfmId()));
			}
			ifmIds1.add(String.valueOf(com.getIfmId()));
		}
		//判断是否传了方案ID
		if (null == ifmIds || ifmIds.size()<=0) {
			if (ifmIds1.size()<=0) {
				return list;
			}
			//根据用户的方案ID查询
			list = mongodb.findGeneralSituation(MongodbDate.class, pageView, ifmIds1, startTime, endTime);
			for (MongodbDate date : list) {
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("articleId", date.getArticleId());
				map.put("ifmId", date.getIfmId());
				map.put("userId", userId);
				//根据方案ID和数据ID查询收藏表
				int count = enshrineMapper.selectByifmIdarticleId(map);
				if (count>0) {
					date.setIsCollection(1);
				} else {
					date.setIsCollection(0);
				}
				//判断是否是微博数据
				if (date.getArticleType().equals(Menu.Micro.getName())) {
					date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
					date.getTitle().trim();
					date.setAuthor(date.getWeiboBlogger());
				}
				for (String ids : ifmIds2) {
					if (date.getIfmId().equals(ids)) {
						//性质
						date.setNature(Menu.Nature2.getName());
						//判断星级 只有负面方案的信息需要判断星级
						if (date.getArticleType().equals(Menu.News.getName()) && date.getReprintNumb()<bs.getFigureTwenty()) {
							//四星
							date.setStarLevel(bs.getFourStar());
						} else {
							if (date.getReprintNumb()<bs.getFigureFive()) {
								//一星
								date.setStarLevel(bs.getOneStar());
							} else if (date.getReprintNumb()>=bs.getFigureFive() && date.getReprintNumb()<bs.getFigureTen()) {
								//两星
								date.setStarLevel(bs.getTwoStar());
							} else if (date.getReprintNumb()>=bs.getFigureTen() && date.getReprintNumb()<bs.getFigureFifteen()) {
								//三星
								date.setStarLevel(bs.getThreeStar());
							} else if (date.getReprintNumb()>=bs.getFigureFifteen() && date.getReprintNumb()<bs.getFigureTwenty()) {
								//四星
								date.setStarLevel(bs.getFourStar());
							} else if (date.getReprintNumb()>=bs.getFigureTwenty()) {
								//五星
								date.setStarLevel(bs.getFiveStar());
							}
						}
					} else {
						if (null == date.getNature() || date.getNature().equals("")) {
							date.setNature(Menu.Nature1.getName());
						}
					}
				}
				if (null == date.getNature()) {
					date.setNature(Menu.Nature1.getName());
				}
				mdlist.add(date);
			}
		} else {
			if (ifmIds.size()<=0) {
				return list1;
			}
			//根据用户ID查询所有方案
			list1 = mongodb.findGeneralSituation(MongodbDate.class, pageView, ifmIds, startTime, endTime);
			for (MongodbDate date : list1) {
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("articleId", date.getArticleId());
				map.put("ifmId", date.getIfmId());
				map.put("userId", userId);
				//根据方案ID和数据ID查询收藏表
				int count = enshrineMapper.selectByifmIdarticleId(map);
				if (count>0) {
					date.setIsCollection(1);
				} else {
					date.setIsCollection(0);
				}
				//判断正负面 判断星级
				for (String ids : ifmIds2) {
					if (date.getIfmId().equals(ids)) {
						date.setNature(Menu.Nature2.getName());
						if (date.getArticleType().equals(Menu.News.getName()) && date.getReprintNumb()<bs.getFigureTwenty()) {
							//四星
							date.setStarLevel(bs.getFourStar());
						} else {
							if (date.getReprintNumb()<bs.getFigureFive()) {
								//一星
								date.setStarLevel(bs.getOneStar());
							} else if (date.getReprintNumb()>=bs.getFigureFive() && date.getReprintNumb()<bs.getFigureTen()) {
								//两星
								date.setStarLevel(bs.getTwoStar());
							} else if (date.getReprintNumb()>=bs.getFigureTen() && date.getReprintNumb()<bs.getFigureFifteen()) {
								//三星
								date.setStarLevel(bs.getThreeStar());
							} else if (date.getReprintNumb()>=bs.getFigureFifteen() && date.getReprintNumb()<bs.getFigureTwenty()) {
								//四星
								date.setStarLevel(bs.getFourStar());
							} else if (date.getReprintNumb()>=bs.getFigureTwenty()) {
								//五星
								date.setStarLevel(bs.getFiveStar());
							}
						}
					} else {
						if (null == date.getNature()) {
							date.setNature(Menu.Nature1.getName());
						}
					}
				}
				if (null == date.getNature()) {
					date.setNature(Menu.Nature1.getName());
				}
				//判断是否是微博数据
				if (date.getArticleType().equals(Menu.Micro.getName())) {
					date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
					date.getTitle().trim();
					date.setAuthor(date.getWeiboBlogger());
				}
				mdlist.add(date);
			}
		}
		return mdlist;
	}
	
	/**
	 * 监控——媒体舆情
	 * @param pageView 分页参数
	 * @param ifmIds 方案ID
	 * @param startTime 开始时间
	 * @param endTime	结束时间
	 * @param articleType 媒体新闻
	 */
	@Override
	public List<MongodbDate> findMedia(PageView pageView,Date startTime,Date endTime,String articleType){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//返回数据集合
		List<MongodbDate> mdlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information com : info) {
			if (com.getIfmIssensitivity()==1) {
				//敏感方案
				ifmIds2.add(String.valueOf(com.getIfmId()));
			} 
			//普通方案
			ifmIds.add(String.valueOf(com.getIfmId()));
		}
		if (ifmIds.size()<=0) {
			return list;
		}
		list = mongodb.findMedia(MongodbDate.class, pageView, ifmIds, startTime, endTime, articleType);
		for (MongodbDate date : list) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("articleId", date.getArticleId());
			map.put("ifmId", date.getIfmId());
			map.put("userId", userId);
			//根据方案ID和数据ID查询收藏表
			int count = enshrineMapper.selectByifmIdarticleId(map);
			if (count>0) {
				date.setIsCollection(1);
			} else {
				date.setIsCollection(0);
			}
			//判断性质
			for (String ids : ifmIds2) {
				if (date.getIfmId().equals(ids)) {
					date.setNature(Menu.Nature2.getName());
					//判断星级 只有负面方案的信息需要判断星级
					if (date.getReprintNumb()>=bs.getFigureTwenty()) {
						//五星
						date.setStarLevel("★★★★★");
					} else {
						date.setStarLevel("★★★★");
					}
				} else {
					if (null == date.getNature()) {
						date.setNature(Menu.Nature1.getName());
					}
				}
			}
			if (null == date.getNature()) {
				date.setNature(Menu.Nature1.getName());
			}
			mdlist.add(date);
		}
		return mdlist;
	}
	
	/**
	 * 监控——网络舆情
	 * @param MongoDate
	 * @param pageView  分页参数
	 * @param ifmIds	方案ID
	 * @param startTime	开始时间
	 * @param endTime	结束时间
	 * @param articleType	网络类型
	 */
	@Override
	public List<MongodbDate> findNetwork(PageView pageView,Date startTime,
			Date endTime,List<String> articleType){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//返回数据集合
		List<MongodbDate> ntlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//敏感方案ID集合
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information com : info) {
			//敏感方案
			if (com.getIfmIssensitivity()==1) {
				ifmIds2.add(String.valueOf(com.getIfmId()));
			} 
			ifmIds.add(String.valueOf(com.getIfmId()));
		}
		if (ifmIds.size()<=0) {
			return list;
		}
		//存信息类型集合
		list = mongodb.findNetwork(MongodbDate.class, pageView, ifmIds, startTime, endTime, articleType);
		for (MongodbDate date : list) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("articleId", date.getArticleId());
			map.put("ifmId", date.getIfmId());
			map.put("userId", userId);
			//根据方案ID和数据ID查询收藏表
			int count = enshrineMapper.selectByifmIdarticleId(map);
			if (count>0) {
				date.setIsCollection(1);
			} else {
				date.setIsCollection(0);
			}
			//判断是否是微博
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				//微博内容放入标题字段中
				date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				//博主放入作者字段中
				date.setAuthor(date.getWeiboBlogger());
			}
			//根据得到的敏感方案ID判断性质
			for (String ids : ifmIds2) {
				//判断正负面
				if (date.getIfmId().equals(ids)) {
					date.setNature(Menu.Nature2.getName());
					//判断星级
					if (date.getReprintNumb()<bs.getFigureFive()) {
						//一星
						date.setStarLevel(bs.getOneStar());
					} else if (date.getReprintNumb()>=bs.getFigureFive() && date.getReprintNumb()<bs.getFigureTen()) {
						//两星
						date.setStarLevel(bs.getTwoStar());
					} else if (date.getReprintNumb()>=bs.getFigureTen() && date.getReprintNumb()<bs.getFigureFifteen()) {
						//三星
						date.setStarLevel(bs.getThreeStar());
					} else if (date.getReprintNumb()>=bs.getFigureFifteen() && date.getReprintNumb()<bs.getFigureTwenty()) {
						//四星
						date.setStarLevel(bs.getFourStar());
					} else if (date.getReprintNumb()>=bs.getFigureTwenty()) {
						//五星
						date.setStarLevel(bs.getFiveStar());
					}
				} else {
					if (null == date.getNature()) {
						date.setNature(Menu.Nature1.getName());
					}
				}
			}
			if (null == date.getNature()) {
				date.setNature(Menu.Nature1.getName());
			}
			ntlist.add(date);
		}
		return ntlist;
	}
	
	/**
	 * 中周舆情搜索
	 */
	@Override
	public List<MongodbDate> findzhongzhouSeek(PageView pageView,
			String articleType, String title,String author) {
		List<MongodbDate> datelist = new ArrayList<MongodbDate>();
		//获取当前用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		List<String> ifmIds = new ArrayList<String>();
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		//循环方案集合
		for (int i = 0; i < info.size(); i++) {
			if (info.get(i).getIfmIssensitivity()==1) {
				ifmIds.add(String.valueOf(info.get(i).getIfmId()));
			}
			ifmIds2.add(String.valueOf(info.get(i).getIfmId()));
		}
		if (ifmIds2.size()<=0) {
			return datelist;
		}
		datelist = mongodb.findzhongzhouSeek(MongodbDate.class, pageView, articleType, title, ifmIds2,author);
		for (MongodbDate date : datelist) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("articleId", date.getArticleId());
			map.put("ifmId", date.getIfmId());
			map.put("userId", userId);
			//根据方案ID和数据ID查询收藏表
			int count = enshrineMapper.selectByifmIdarticleId(map);
			if (count>0) {
				date.setIsCollection(1);
			} else {
				date.setIsCollection(0);
			}
			//判断是否是微博
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				//微博内容放入标题字段中
				date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				//博主放入作者字段中
				date.setAuthor(date.getWeiboBlogger());
			}
			//根据得到的敏感方案ID判断性质
			for (String ids : ifmIds) {
				//判断正负面
				if (date.getIfmId().equals(ids)) {
					//负面
					date.setNature(Menu.Nature2.getName());
				} else {
					//正面
					if (null == date.getNature()) {
						date.setNature(Menu.Nature1.getName());
					}
				}
			}
			//正面
			if (null == date.getNature()) {
				date.setNature(Menu.Nature1.getName());
			}
			list.add(date);
		}
		return list;
	}

	/**
	 * 查询微信单条数据
	 */
	@Override
	public MongodbDate findSingle(String articleId, String ifmId) {
		MongodbDate date = mongodb.findSingle(MongodbDate.class, articleId, ifmId);
		return date;
	}
	/**
	 * Id集合查询
	 */
	@Override
	public List<MongodbDate> findByArticleId(List<String> Id) {
		List<String> ifmIds = new ArrayList<String>();
		List<String> ifmIds2 = new ArrayList<String>();
		List<MongodbDate> datelist = new ArrayList<MongodbDate>();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		//循环方案集合
		for (int i = 0; i < info.size(); i++) {
			if (info.get(i).getIfmIssensitivity()==1) {
				ifmIds.add(String.valueOf(info.get(i).getIfmId()));
			}
			ifmIds2.add(String.valueOf(info.get(i).getIfmId()));
		}	
		List<MongodbDate> list = mongodb.findByArticleId(MongodbDate.class, Id);
		for (MongodbDate date : list) {
			//判断是否为微博
			if (date.getArticleType().equals(Menu.Micro.name())) {
				//去标签，微博内容set到title字段
				date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
				//去掉前后空格
				date.getTitle().trim();
				//微博博主set到author字段
				date.setAuthor(date.getWeiboBlogger());
				date.setWebsiteName("微博");
			} else if (date.getArticleType().equals(Menu.WeiXin.name())) {
				date.setWebsiteName("微信");
			} else if (date.getArticleType().equals(Menu.BBS.name())) {
				date.setWebsiteName("论坛");
			}
			//根据得到的敏感方案ID判断性质
			for (String ids : ifmIds) {
				//判断正负面
				if (date.getIfmId().equals(ids)) {
					//负面
					date.setNature(Menu.Nature2.getName());
				} else {
					//正面
					if (null == date.getNature()) {
						date.setNature(Menu.Nature1.getName());
					}
				}
			}
			//正面
			if (null == date.getNature()) {
				date.setNature(Menu.Nature1.getName());
			}
			datelist.add(date);
		}
		return datelist;
	}

}
